//
//  KeychainViewController.m
//  KeychainDemo
//
//  Created by Guo JingRui on 2017/11/23.
//  Copyright © 2017年 Guo JingRui. All rights reserved.
//

#import "KeychainViewController.h"
#import "YKeychain.h"
#import "YTestObject.h"

static NSString * const accessGroup = @"HBYY.KeychainDemo";

@interface KeychainViewController ()

@end

@implementation KeychainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"钥匙串存取信息";
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [YKeychain setValue:@(123456) forKey:@"userID"];
    [YKeychain setValue:@"123456" forKey:@"account"];
    
    NSInteger userID = [[YKeychain valueForKey:@"userID"] integerValue];
    NSString *account = [YKeychain valueForKey:@"account"];
    NSLog(@"userID:%zi  account:%@",userID,account);
    
    [YKeychain setValue:@"HBYY.KeychainDemo" forKey:@"AccessGroup" forAccessGroup:accessGroup];
    NSString *group = [YKeychain valueForKey:@"AccessGroup" forAccessGroup:accessGroup];
    NSLog(@"group:%@",group);
    
    NSString *uuid = [self getUUID];
    NSLog(@"uuid %@",uuid);
    
    YTestObject *object = [YTestObject new];
    object.string = @"123456";
    
    [YKeychain setValue:object forKey:@"customObject"];
    YTestObject *obj = [YKeychain valueForKey:@"customObject"];
    NSLog(@"obj %@",obj.string);
}


- (NSString *)getUUID{
    NSString *uuid = [YKeychain valueForKey:@"YDeviceUUID"];
    if (uuid == nil) {
        uuid = [NSUUID UUID].UUIDString;
        [YKeychain setValue:uuid forKey:@"YDeviceUUID"];
    }
    return uuid;
}


@end
